package com.guguskins.back.controller;


import com.guguskins.back.common.annotation.Authorized;
import com.guguskins.back.common.constant.InventoryConstant;
import com.guguskins.back.entity.*;
import com.guguskins.back.entity.vo.ResultVo;
import com.guguskins.back.entity.vo.TurntableVo;
import com.guguskins.back.service.*;
import com.guguskins.back.utils.DateUtil;
import com.guguskins.back.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * <p>
 * 转盘表 前端控制器
 * </p>
 *
 * @author mybatis-plus
 * @since 2021-07-14
 */
@RestController
@RequestMapping("/guTurntable")
public class GuTurntableController {
    @Autowired
    private GuTurntableService turntableService;
    @Autowired
    private GuInventoryService inventoryService;
    @Autowired
    private GuPrizeRecordService prizeRecordService;
    @Autowired
    private GuDisCountOrUserService disCountOrUserService;

    @Autowired
    private GuUserService userService;

    @PostMapping("/back/insert")
    public ResultVo backInsert(@RequestBody GuTurntable turntable) {
        if(turntable.getTtType()==null){
            return ResultVo.fail("奖品类型不能为空");
        }
        if(turntable.getTtPrizeType()==null){
            return ResultVo.fail("奖品不能为空");
        }
        if(turntable.getTtOpenDoor()==null){
         turntable.setTtType(2);
        }
        if(turntable.getTtProbability()==null){
            return ResultVo.fail("抽奖概率不能为空");
        }
        turntableService.save(turntable);
        return ResultVo.success("保存成功");
    }
    @GetMapping("back/list")
    public ResultVo backList(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize){

       return ResultVo.success( turntableService.findByList(pageNum, pageSize));
    }
    @GetMapping("/list")
    public ResultVo backList(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize,
                             HttpServletRequest request

    ) {
        String token = request.getHeader("token");
        Long userId = JwtUtil.verifyToken(token).get("userId",Long.class);

        return ResultVo.success(turntableService.selectListTurntable(pageNum, pageSize,Integer.parseInt(userId.toString())), "查找成功");
    }

    @PostMapping("/back/update")
    public ResultVo backUpdate(@RequestBody GuTurntable guTurntable) {
        turntableService.updateById(guTurntable);
        return ResultVo.success("修改成功");
    }

    @GetMapping("/back/delect")
    public ResultVo backDelect(Integer id, Integer enable) {
        GuTurntable turntable=new GuTurntable();
        turntable.setId(id);

            turntable.setTtEnable(enable);

        turntableService.updateById(turntable);
        return ResultVo.success("状态更新成功");
    }

    /**
     * guo
     * 用户抽奖添加仓库方法
     */

    @GetMapping("/insertInvenToryOrUser")
    @Authorized
    public ResultVo insertInvenToryOrUser(
            HttpServletRequest request) {
        int turntableId = turntableService.insertUserinfoProbability();
        GuTurntable guTurntable = turntableService.findById(turntableId);


        String token = request.getHeader("token");
        Long userId = JwtUtil.verifyToken(token).get("userId",Long.class);

        GuUser user = userService.getById(userId);

        if (user.getUserRevolutions() > 0) {
            user.setUserRevolutions(user.getUserRevolutions()-1);
            userService.save(user);
            inventoryService.addInventory(userId, null, guTurntable.getTtPrizeType(), InventoryConstant.Source.DAILY_LOTTERY, (long) guTurntable.getId(), (long) guTurntable.getTtPrizeId(), null);
            turntableService.insertInvenToryOrUser(Integer.parseInt(userId.toString()), turntableId);
            TurntableVo turntableVo = new TurntableVo();
            if (guTurntable.getGuBox() != null) {
                turntableVo.setItemName(guTurntable.getGuBox().getBoxName());
                turntableVo.setItemPrice(guTurntable.getGuBox().getBoxPrice());
                turntableVo.setItemSubImage(guTurntable.getGuBox().getBoxImageUrl());
                return ResultVo.success(turntableVo, "抽奖成功");
            } else if (guTurntable.getGuGoods() != null) {
                turntableVo.setItemName(guTurntable.getGuGoods().getGoodsName());
                turntableVo.setItemPrice(guTurntable.getGuGoods().getGoodsPrice());
                turntableVo.setItemSubImage(guTurntable.getGuGoods().getGoodsIconUrl());
                turntableVo.setItemImage(guTurntable.getGuGoods().getGoodsOriginalIconUrl());
                return ResultVo.success(turntableVo, "抽奖成功");
            } else if (guTurntable.getGuProp() != null) {
                turntableVo.setItemName(guTurntable.getGuProp().getPropName());
                turntableVo.setItemSubImage(guTurntable.getGuProp().getPropImage());
                return ResultVo.success(turntableVo, "抽奖成功");
            } else {
                GuDisCountOrUser guDisCountOrUser = new GuDisCountOrUser();
                guDisCountOrUser.setDuUserId(Integer.parseInt(userId.toString()));
                guDisCountOrUser.setDuDisCountId(guTurntable.getGuDiscount().getDcId());
                guDisCountOrUser.setDuCounponCode(UUID.randomUUID().toString().replaceAll("-",""));
                guDisCountOrUser.setDuBeOverdue(DateUtil.getTimeAddHours(guTurntable.getGuDiscount().getDcBeOverdue()).toString());
                disCountOrUserService.save(guDisCountOrUser);
                turntableVo.setItemName(guTurntable.getGuDiscount().getDcTitle());
                turntableVo.setItemLevel(user.getUserRevolutions());
                turntableVo.setItemSubImage(guTurntable.getGuDiscount().getDcImages());
                return new ResultVo("success",turntableVo, "抽奖成功");
            }
        }
                   return new ResultVo("fail",null,"抽奖次数已用完");
        }


    }

